package com.lenovo.translatetext.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @ProjectName: TranslateText
 * @Package: com.lenovo.translatetext
 * @ClassName: MD5
 * @Description: MD5类作用描述
 * @Author: kgzhou
 * @CreateDate: 2019/4/19 9:53
 * @UpdateUser: kgzhou
 * @UpdateDate: 2019/4/19 9:53
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class MD5 {
    private static final String TAG = MD5.class.getSimpleName();
//    //十六进制下数字到字符的映射数组
//    private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
//
//    /**
//     * 把inputString加密
//     */
//    public static String md5(String inputStr) {
//        return encodeByMD5(inputStr);
//    }
//
//    /**
//     * 对字符串进行MD5编码
//     */
//    private static String encodeByMD5(String originString) {
//        Log.d(TAG, "encodeByMD5: originString--->" + originString);
//        if (originString != null) {
//            try {
//                //创建具有指定算法名称的信息摘要
//                MessageDigest md5 = MessageDigest.getInstance("MD5");
//                //
//                md5.update(originString.getBytes("UTF-8"));
//                //使用指定的字节数组对摘要进行最后更新，然后完成摘要计算
//                byte[] results = md5.digest(originString.getBytes());
//                //将得到的字节数组变成字符串返回
//                String result = byteArrayToHexString(results);
//                return result;
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        return null;
//    }
//
//    /**
//     * 轮换字节数组为十六进制字符串
//     *
//     * @param b 字节数组
//     * @return 十六进制字符串
//     */
//    private static String byteArrayToHexString(byte[] b) {
//        StringBuffer resultSb = new StringBuffer();
//        for (int i = 0; i < b.length; i++) {
//            resultSb.append(byteToHexString(b[i]));
//        }
//        return resultSb.toString();
//    }
//
//    //将一个字节转化成十六进制形式的字符串
//    private static String byteToHexString(byte b) {
//        int n = b;
//        if (n < 0)
//            n = 256 + n;
//        int d1 = n / 16;
//        int d2 = n % 16;
//        return hexDigits[d1] + hexDigits[d2];
//    }

    //MD5加密，只有该方法加密有效，以上方法签名有误
    public static String getMD5(String info) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(info.getBytes("UTF-8"));
            byte[] encryption = md5.digest();

            StringBuffer strBuf = new StringBuffer();
            for (int i = 0; i < encryption.length; i++) {
                if (Integer.toHexString(0xff & encryption[i]).length() == 1) {
                    strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));
                } else {
                    strBuf.append(Integer.toHexString(0xff & encryption[i]));
                }
            }
            return strBuf.toString();
        } catch (NoSuchAlgorithmException e) {
            return "";
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }


    //------------------------获取32位应用签名方法-------------------------------

    private static final char[] hexDigits = {48, 49, 50, 51, 52, 53, 54, 55,
            56, 57, 97, 98, 99, 100, 101, 102};

    public static String hexdigest(String paramString) {
        try {
            String str = hexdigest(paramString.getBytes());
            return str;
        } catch (Exception localException) {
        }
        return null;
    }

    public static String hexdigest(byte[] paramArrayOfByte) {
        try {
            MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
            localMessageDigest.update(paramArrayOfByte);
            byte[] arrayOfByte = localMessageDigest.digest();
            char[] arrayOfChar = new char[32];
            int i = 0;
            int j = 0;
            while (true) {
                if (i >= 16)
                    return new String(arrayOfChar);
                int k = arrayOfByte[i];
                int m = j + 1;
                arrayOfChar[j] = hexDigits[(0xF & k >>> 4)];
                j = m + 1;
                arrayOfChar[m] = hexDigits[(k & 0xF)];
                i++;
            }
        } catch (Exception localException) {
        }
        return null;
    }


}
